import { useDigitalTwinStore } from '@/stores/digitalTwin'
const digitalTwinStore = useDigitalTwinStore()

const enableDecalList = ['苏州地形影像_Terrain', '【cim2】苏州白模', '【cim2】苏州工业园区', '【cim2】姑苏区', '【cim2】核心区_去高程', 'SuZhou_JJH_XP', '苏州建筑A01', '孪创城市植被', '苏州金鸡湖地形']

export const initTrafficJam = async () => {
  fdapi.camera.set([566398.849688, 3465370.433438, 6176.095625, -79.461441, -97.873497, 2])

  // 1、热力图添加  heatmap
  // 2、分离道路模型
  // 3、道路模型贴合设置  tileLayer.enableDecal()

  const infoTreeObj = digitalTwinStore.infoTreeObj
  await fdapi.tileLayer.enableDecal(
    enableDecalList.map(name => {
      return {
        tileLayerId: infoTreeObj[name],
        supportAttach: false
      }
    })
  )

  let bbox = [559642.49, 3462732.8, 0, 572580.585, 3470417.6, 300]
  let range = [0, 100]
  let data = []
  for (let i = 0; i < 2000; i++) {
    let x = getRandNumBetween(bbox[0], bbox[3]) //minX ~ maxX
    let y = getRandNumBetween(bbox[1], bbox[4]) //minY ~ maxY
    data.push({
      id: i.toString(),
      coordinate: [x, y, 0], //热力点的坐标
      radius: 300, //热力点影像半径范围
      heatValue: 1 //热力值
    })
  }
  await fdapi.heatmap.add('exitTraffic_heatmap', bbox, range, data)
}
export const exitTrafficJam = async () => {
  await fdapi.heatmap.delete('exitTraffic_heatmap')

  const infoTreeObj = digitalTwinStore.infoTreeObj
  await fdapi.tileLayer.enableDecal(
    enableDecalList.map(name => {
      return {
        tileLayerId: infoTreeObj[name],
        supportAttach: true
      }
    })
  )
}

function getRandNumBetween(min: number, max: number) {
  return Math.round((Math.random() * (max - min) + min) * 10) / 10
}
